package com.daylive.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;







import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.daylive.model.Category;
import com.daylive.page.Page;
import com.daylive.service.CategoryService;


@Controller
@RequestMapping("category")
public class CategoryController {
	private final Logger logger = Logger.getLogger(CategoryController.class);
	
	@Autowired
	private CategoryService categoryService;
	public static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = getRequest().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		return attrs.getRequest();
	}
	private void clearSession() {
		getSession().invalidate();
	}

	@RequestMapping("/category_list.do")
	public ModelAndView getCategoryList() {
		return getCategoryList(1);
	}
	@RequestMapping("/category_page_list.do")
	public ModelAndView getCategoryList(@RequestParam int pageNumber) {
		Page<Category> pageList = categoryService.getCategoryList(pageNumber);
		ModelAndView mv = new ModelAndView("category_list");
		mv.addObject("currentPage", pageNumber);
		mv.addObject("totalPage", pageList.getTotalPage());
		mv.addObject("totalCount", pageList.getTotalCount());
		mv.addObject("categoryList", pageList.getResultList());
		return mv;
	}
	 @RequestMapping("/category_add.do")
		public ModelAndView addCategory() {
			ModelAndView mv = new ModelAndView();
			mv.setViewName("category_add");
			return mv;
		}
	   @RequestMapping("/category_save.do")
	   	public ModelAndView savecategory(Category category) {
	   		ModelAndView mv = new ModelAndView();
	   		int rel = categoryService.addCategory(category);
	   		mv.setViewName("success");
			mv.addObject("msg",rel > 0 ? "数据添加成功！" : "数据添加失败！");
			return mv;   		
	   	}
	  @RequestMapping("/category_edit.do")
		public ModelAndView editCategory(@RequestParam("id")int id) {
		  Category category = categoryService.getCategoryById(id);
			ModelAndView mv = new ModelAndView();
			mv.addObject("category", category);
			mv.setViewName("category_edit");
			return mv;
		}
	    @RequestMapping("/category_update.do")
	   	public ModelAndView updateCategory(Category category) {
	   		ModelAndView mv = new ModelAndView();
	   		int rel =categoryService.updateCategory(category);
	   		mv.setViewName("success");
			mv.addObject("msg",rel > 0 ? "数据修改成功！" : "数据修改失败！");
			return mv;   		
	   	}
	    @RequestMapping("/category_del.do")
		public String delCategory(@RequestParam("id")int id) {
	    	categoryService.delCategory(id);
			return "success";
		}
		@RequestMapping("/category_delAll.do")
		public String deleteAll(String ids) {
			String[] array_IDS = ids.split(",");
			categoryService.delAll(array_IDS);
			return "success";
		}
}